package com.hsy.crm.web.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Customer;
import com.hsy.crm.web.domain.entity.ReceiptOrder;
import com.hsy.crm.web.domain.entity.VerificateOrder;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.customer.CustomerPagePo;
import com.hsy.crm.web.domain.po.customer.CustomerPo;
import com.hsy.crm.web.domain.vo.customer.CustomerPageVo;
import com.hsy.crm.web.mapper.CustomerMapper;
import com.hsy.crm.web.mapper.ReceiptOrderMapper;
import com.hsy.crm.web.mapper.VerificateOrderMapper;
import com.hsy.crm.web.service.base.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

@Service
public class CustomerService extends BaseService<Customer> {

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    ReceiptOrderMapper receiptOrderMapper;

    @Autowired
    VerificateOrderMapper verificateOrderMapper;

    @Transactional
    public Unified<Integer> insertCustomer(Customer customer) {
        Unified<Integer> unified = new Unified<>();

        if( customer.getBalance() == null ){
            customer.setBalance(0l);
        }

        if(repeat_check_insert(customer)){
            unified.setResult(App.BussError.CODE_REPEAT.getCode());
        }else{
            unified.setData(this.insert(customer,Boolean.TRUE));
        }
        return unified;
    }

    public PageResult<CustomerPagePo> page(CustomerPageVo customerPageVo) {
        Page page = PageHelper.startPage(customerPageVo.getPage(),customerPageVo.getLimit());
        PageResult<CustomerPagePo> pageResult = new PageResult<>();

        if( customerPageVo.getCompanyId() == null ){
            customerPageVo.setCompanyId(customerPageVo.getLoginCompanyId());
        }

        pageResult.setRows(customerMapper.page(customerPageVo));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public Unified<Integer> deleteCustomer(Customer customer) {

        Long id = customerMapper.checkOrder(customer);
        if( id != null ){
            throw new ServiceException(App.BussError.CUSTOMER_DELETE_ORDER.getCode());
        }

        //receipt
        ReceiptOrder receiptOrder = new ReceiptOrder();
        receiptOrder.setCustomerId(customer.getCustomerId());
        receiptOrder.setRelationId(customer.getId());
        Long receiptId = receiptOrderMapper.checkOrder(receiptOrder);
        if( receiptId != null ){
            throw new ServiceException(App.BussError.CUSTOMER_DELETE_RECEIPT.getCode());
        }

        // verificate
        VerificateOrder verificateOrder = new VerificateOrder();
        verificateOrder.setCustomerId(customer.getCustomerId());
        verificateOrder.setRelationId1(customer.getId());
        Long verificateId = verificateOrderMapper.checkCustomer(verificateOrder);
        if( verificateId != null ){
            throw new ServiceException(App.BussError.CUSTOMER_DELETE_HX.getCode());
        }

        return Unified.newInstance(this.delete(customer));
    }

    public Unified<CustomerPo> getCustomer(Customer customer) {
        Unified<CustomerPo> unified = new Unified<>();
        unified.setData(customerMapper.getCustomer(customer));
        return unified;
    }

    @Transactional
    public Unified<Integer> updateCustomer(Customer customer) {
        Unified<Integer> unified = new Unified<>();
        if(repeat_check_update(customer)){
            unified.setResult(App.BussError.CODE_REPEAT.getCode());
        }else{
            unified.setData(this.update(customer,Boolean.TRUE));
        }
        return unified;
    }

    private boolean repeat_check_insert(Customer customer){
        Example example = new Example(Customer.class);
        example.setTableName( customer.getDynamicTableName() );
        Example.Criteria criteria = example.createCriteria();
        if( StringUtils.isEmpty(customer.getCode()) ){
            throw new ServiceException("bean field property name is null");
        }
        criteria.andEqualTo(App.Property.CODE,customer.getCode());
        int count = customerMapper.selectCountByExample(example);

        return count > 0;
    }

    private boolean repeat_check_update(Customer customer){
        Example example = new Example(Customer.class);
        example.setTableName( customer.getDynamicTableName() );
        Example.Criteria criteria = example.createCriteria();
        if( StringUtils.isEmpty(customer.getCode()) || customer.getCompanyId() == null || customer.getId() == null ){
            throw new ServiceException("bean field property code,customerId,companyId is null");
        }
        criteria.andEqualTo(App.Property.CODE,customer.getCode());
        criteria.andEqualTo(App.Property.COMPNAY_ID,customer.getCompanyId());
        criteria.andNotEqualTo(App.Property.ID,customer.getId());
        int count = customerMapper.selectCountByExample(example);

        return count > 0;
    }

}
